/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import com.sun.net.ssl.internal.ssl.Debug;
import java.awt.Color;

/**
 *
 * @author Carlo
 */
public class editProduct extends javax.swing.JDialog {

    private product prod;
    private int stock;

    /**
     * Creates new form editProduct
     */
    public editProduct(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }

    public void setProduct(product prod) {
        this.prod = prod;
        priceField.setText(Float.toString(prod.getPrice()));
        this.stock = prod.getCurrentStock();
        stockLabel.setText(Integer.toString(stock));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        priceField = new javax.swing.JTextField();
        closeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        stockField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        minusStockButton = new javax.swing.JButton();
        addStockButton = new javax.swing.JButton();
        submitButton = new javax.swing.JButton();
        stockLabel = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Product");

        jLabel5.setText("Price:");

        priceField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                priceFieldFocusGained(evt);
            }
        });

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("PHP");

        jLabel2.setText("Stock:");

        stockField.setText("1");
        stockField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                stockFieldFocusGained(evt);
            }
        });

        minusStockButton.setText(" - ");
        minusStockButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minusStockActionPerformed(evt);
            }
        });

        addStockButton.setText("+");
        addStockButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addStockActionPerformed(evt);
            }
        });

        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        stockLabel.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                stockLabelFocusGained(evt);
            }
        });

        jLabel3.setText("Increment:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(priceField, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel1))
                            .addComponent(stockLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(submitButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(closeButton))
                                    .addComponent(stockField, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addStockButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(minusStockButton))))
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel5))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(priceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(stockLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(stockField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(addStockButton)
                        .addComponent(minusStockButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(19, 19, 19))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(submitButton)
                            .addComponent(closeButton, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        
        boolean syntaxCheck = true;
        
        if (!(stockLabel.getText().isEmpty())) {
            try {
                stock = Integer.parseInt(stockLabel.getText());
            } catch (Exception ex) {        
                syntaxCheck = false;
                stockLabel.setBackground(Color.PINK);
                stockLabel.setText("Please input numeric characters");
            }
        }
        
        if (stockLabel.getText().isEmpty()) {
            syntaxCheck = false;
            stockLabel.setBackground(Color.PINK);
            stockLabel.setText("Stock cannot be empty");
        }
        
        if(stock < 0 && syntaxCheck){
            syntaxCheck = false;
            stockLabel.setBackground(Color.PINK);
            stockLabel.setText("Please input a positive number");
        }
        
        //Error Check for price
        float price = 0;
        if (!(priceField.getText().isEmpty())) {
            try{
                price = Float.parseFloat(priceField.getText());
            } catch (NumberFormatException ex) {
                syntaxCheck = false;
                priceField.setBackground(Color.PINK);
                priceField.setText("Please input numeric characters");
            }
        }
        
        Debug.println("price:", String.valueOf(price));
        
        if (priceField.getText().isEmpty()) {
            syntaxCheck = false;
            priceField.setBackground(Color.PINK);
            priceField.setText("Price cannot be empty");
        }
        
        if(price < 0 && syntaxCheck){
            syntaxCheck = false;
            priceField.setBackground(Color.PINK);
            priceField.setText("Please input a positive number");
        }
        
        //Check if textboxes were edited for errors
        if (stockLabel.getBackground().equals(Color.PINK) || priceField.getBackground().equals(Color.PINK)) {
            syntaxCheck = false;
        }
     
        if(syntaxCheck){
            confirmDialogue dialog = new confirmDialogue(null, true);
            dialog.setVisible(true);
            
            if(dialog.getReturnStatus() == 1){
                prod.setProductPrice(price);
                prod.updateProductStock(stock);
                positiveMessage posMsg = new positiveMessage(null, true);
                posMsg.setMessage("Product Edited successfully");
                posMsg.setVisible(true);
                this.dispose();
            }
        }

    }//GEN-LAST:event_submitButtonActionPerformed

    private void minusStockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minusStockActionPerformed
        boolean syntaxCheck = true;
        try {
            stock -= Integer.parseInt(stockField.getText());
        } catch (Exception ex) {
            syntaxCheck = false;
            stockField.setBackground(Color.PINK);
            stockField.setText("Please input numeric characters");
        }
        
        if(syntaxCheck){
            stockLabel.setText(Integer.toString(stock));
        }
    }//GEN-LAST:event_minusStockActionPerformed

    private void addStockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addStockActionPerformed
        boolean syntaxCheck = true;
        try {
            stock += Integer.parseInt(stockField.getText());
        } catch (Exception ex) {
            syntaxCheck = false;
            stockField.setBackground(Color.PINK);
            stockField.setText("Please input numeric characters");
        }
        
        if(syntaxCheck){
            stockLabel.setText(Integer.toString(stock));
        }
    }//GEN-LAST:event_addStockActionPerformed

    private void priceFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_priceFieldFocusGained
        if (priceField.getBackground().equals(Color.PINK)) {
            priceField.setText(Float.toString(prod.getPrice()));
            priceField.setBackground(new Color(255, 255, 255));
        }
    }//GEN-LAST:event_priceFieldFocusGained

    private void stockLabelFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_stockLabelFocusGained
        if (stockLabel.getBackground().equals(Color.PINK)) {
            stockLabel.setText(Integer.toString(this.stock));
            stockLabel.setBackground(new Color(255, 255, 255));
        }
    }//GEN-LAST:event_stockLabelFocusGained

    private void stockFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_stockFieldFocusGained
        if (stockField.getBackground().equals(Color.PINK)) {
            stockField.setText("");
            stockField.setBackground(new Color(255, 255, 255));
        }
    }//GEN-LAST:event_stockFieldFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(editProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(editProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(editProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(editProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                editProduct dialog = new editProduct(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addStockButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton minusStockButton;
    private javax.swing.JTextField priceField;
    private javax.swing.JTextField stockField;
    private javax.swing.JTextField stockLabel;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables
}
